package com.reggie.controller;

import com.reggie.dto.DishDto;
import com.reggie.dto.SetmealDto;
import com.reggie.pojo.*;
import com.reggie.service.SetmealDishService;
import com.reggie.service.SetmealService;
import com.reggie.util.PageUtil;
import com.reggie.util.ResultUtil;
import com.reggie.util.SnowflakeIdWorkerUtil;
import com.reggie.util.ThreadLocalUtil;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//套餐请求映射类
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;
    @Autowired
    SetmealDishService setmealDishService;

    //分页查询多条套餐信息
    @GetMapping("/page")
    public ResultUtil<PageUtil<Setmeal>> page(@RequestParam(value = "page", defaultValue = "0") int page,
                                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                              @RequestParam(value = "name", required = false) String name) {
        PageUtil<Setmeal> setmealPage = setmealService.findAll(page, pageSize, name);
        if (setmealPage == null) return ResultUtil.error("未查询到套餐！");
        return ResultUtil.success(setmealPage);
    }

    //删除套餐信息
    @DeleteMapping
    public ResultUtil<Null> delete(String ids) {
        Map<String, Object> map = ThreadLocalUtil.get();

        Boolean isSuccess = true;
        for (String i : ids.split(",")) {
            Long id = Long.valueOf(i);
            if (setmealService.delete(id, Long.valueOf((String) map.get("id")))) {
                isSuccess = true;
                continue;
            }
            isSuccess = false;
        }
        if (isSuccess) return ResultUtil.success(null);
        return ResultUtil.error("删除套餐失败！");
    }

    //根据套餐状态和id修改套餐状态
    @PostMapping("status/{status}")
    private ResultUtil<Null> status(@PathVariable("status") Integer status, String ids) {
        System.out.println(status);
        Map<String, Object> map = ThreadLocalUtil.get();
        Boolean isSuccess = true;
        for (String i : ids.split(",")) {
            Long id = Long.valueOf(i);
            if (setmealService.editStatus(id, status, Long.valueOf((String) map.get("id")))) continue;
            isSuccess = false;
        }
        if (isSuccess) return ResultUtil.success(null);
        return ResultUtil.error("修改套餐状态失败！");
    }

    //添加套餐信息
    @PostMapping
    public ResultUtil<Null> add(@RequestBody SetmealDto setmealDto) {
        Map<String, Object> map = ThreadLocalUtil.get();
        setmealDto.setCreateUser(Long.valueOf((String) map.get("id")));
        setmealDto.setUpdateUser(Long.valueOf((String) map.get("id")));
        Long id = new SnowflakeIdWorkerUtil(0, 0).nextId();
        setmealDto.setId(id);
        Boolean isSuccess = true;
        if (setmealDto.getSetmealDishes() != null) {
            for (SetmealDish setmealDish : setmealDto.getSetmealDishes()) {
                Long setmealDishId = new SnowflakeIdWorkerUtil(0, 0).nextId();
                setmealDish.setId(setmealDishId);
                setmealDish.setSort(0);
                setmealDish.setSetmealId(String.valueOf(setmealDto.getId()));
                setmealDish.setCreateUser(Long.valueOf((String) map.get("id")));
                setmealDish.setUpdateUser(Long.valueOf((String) map.get("id")));
                if (setmealDishService.add(setmealDish)) continue;
                isSuccess = false;
            }
        }
        if (setmealService.add(setmealDto) && isSuccess) return ResultUtil.success(null);
        return ResultUtil.error("添加套餐失败！");
    }

    //根据id获取单条套餐信息
    @GetMapping("/{id}")
    public ResultUtil<SetmealDto> find(@PathVariable("id") Long id){
        SetmealDto setmealDto = setmealService.find(id);
        if (setmealDto==null) return ResultUtil.error("查询套餐失败！");
        List<SetmealDish> setmealDishes = setmealDishService.find(id);
        setmealDto.setSetmealDishes(setmealDishes);
        return ResultUtil.success(setmealDto);
    }

    //编辑套餐信息
    @PutMapping
    public ResultUtil<Null> edit(@RequestBody SetmealDto setmealDto){
        Map<String, Object> map = ThreadLocalUtil.get();
        Boolean isSuccess = true;
        setmealDto.setUpdateUser(Long.valueOf((String) map.get("id")));
        for (SetmealDish setmealDish:setmealDto.getSetmealDishes()){
            if(setmealDishService.delete(setmealDish.getId(), Long.valueOf((String) map.get("id"))));
            Long setmealDishId = new SnowflakeIdWorkerUtil(0, 0).nextId();
            setmealDish.setId(setmealDishId);
            setmealDish.setSetmealId(String.valueOf(setmealDto.getId()));
            setmealDish.setSort(0);
            setmealDish.setCreateUser(Long.valueOf((String) map.get("id")));
            setmealDish.setUpdateUser(Long.valueOf((String) map.get("id")));
            if (setmealDishService.add(setmealDish)) continue;
            isSuccess = false;
        }
        if (setmealService.edit(setmealDto)&&isSuccess) return ResultUtil.success(null);
        return ResultUtil.error("编辑套餐失败！");
    }

    //根据categoryId和status查询多条套餐信息
    @GetMapping("/list")
    public ResultUtil<List<Setmeal>> findByCategoryIdAndStatus(@RequestParam("categoryId") Long categoryId, @RequestParam("status") Integer status){
        List<Setmeal> setmeals = setmealService.findByCategoryIdAndStatus(categoryId, status);
        if (setmeals==null) return ResultUtil.error("查询套餐失败！");
        return ResultUtil.success(setmeals);
    }
}
